package net.csdn.www;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

/**
 * 类的说明：从指定的url获取数据，打印符合要求的数据。<br>
 * 
 * @author liuzd
 * 
 */
public class Test01 {

	private static String URL = null;
	private static String[] IMG_REGEXS = null;
	private static String HTTP_REGEX = null;
	private static boolean WEB_PAGE_FLAG = false;
	private static int WEB_PAGE_MAX = 1;
	private static int WEB_PAGE_MIN = 1;
	private static int count = 0;
	
	private static String dateStr = null;
	
	static {
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		dateStr = dateFormat.format(now) + "==";
	}

	private static Properties getPropertiesFile(URI uri) {
		Properties defaultProperties  = null;
		try {
			File file = new File(uri);
			FileInputStream defaultInputStream = new FileInputStream(file);
			defaultProperties = new Properties();
			defaultProperties.load(defaultInputStream);
		} catch (IOException e) {
			System.out.println("ERROR:==load properties file fail==");
			System.out.println("THE SPECIFIC ERROR INFO : " + e);
			e.printStackTrace();
		}
		return defaultProperties;
	}
	
	private static void getPropertys(URI uri){
		Properties defaultProperties = getPropertiesFile(uri);
		URL = defaultProperties.getProperty("url");
		HTTP_REGEX = defaultProperties.getProperty("http.regex");
		IMG_REGEXS = getStringArrayFromStr(defaultProperties.getProperty("image.regex"),";");
		WEB_PAGE_FLAG = new Boolean(defaultProperties.getProperty("web.page.flag"));
		WEB_PAGE_MAX = Integer.parseInt(defaultProperties.getProperty("web.page.max"));
		WEB_PAGE_MIN = Integer.parseInt(defaultProperties.getProperty("web.page.min"));
	}
	
	private static String[] getStringArrayFromStr(String str,String regex){
		String[] strs = str.split(regex);
		return strs;
	}

	private static URI getRealClassPath(){
		URI uri = null;
		try {
			uri = Test01.class.getResource("/el.properties").toURI();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return uri;
	}
	
	
	private static void start(){
		// init the attribute
		getPropertys(getRealClassPath());
		// start the program
		if(WEB_PAGE_FLAG){
			if(WEB_PAGE_MAX < WEB_PAGE_MIN){
				System.out.println("if you want to page ,you must appoint the start page number and the max page number");
				return ;
			}
			for (int i = WEB_PAGE_MIN; i < WEB_PAGE_MAX; i++) {
				String str = "page=" + i;
				FileUtli.outPutStringToFile(str);
				String url = URL + i;
				printUrl(url);
			}
		}else{
			printUrl(URL);
		}
	}

	private static void printUrl(String urlStr) {
		List<String> imagePageUrlList = obtainAllPostTitle(urlStr);
		Set<String> set = new HashSet<String>(imagePageUrlList);
		for (String str : set) {
			FileUtli.outPutStringToFile(str);
			printList(getImgs(str));
		}
	}

	/**
	 * 从指定的url中获取该url中所有的帖子标题 刘柱栋 2013-3-4
	 * 
	 * @param url
	 * @return
	 */
	private static List<String> obtainAllPostTitle(String urlStr) {
		List<String> list = new ArrayList<String>();
		URL url = null;
		URLConnection conn = null;
		String nextLine = null;
		StringTokenizer tokenizer = null;
		try {
			// 获得网页资源
			url = new URL(urlStr);
			// 获得资源连接
			conn = url.openConnection();
			conn.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			// 开始读取网页信息解析出网页中的超链接
			while ((nextLine = reader.readLine()) != null) {
				tokenizer = new StringTokenizer(nextLine);
				while (tokenizer.hasMoreTokens()) {
					String urlToken = tokenizer.nextToken();
//					System.out.println("===========================================");
//					System.out.println(urlToken);
//					if (isUrl(urlToken)) {
						if (isMatcher(HTTP_REGEX, urlToken)) {
							list.add(getHttp(urlToken));
						}
//					}
				}
			}

		} catch (Exception e) {
			System.out.println("==[ hava error ]==" + e);
			e.printStackTrace();
		}
		return list;
	}

	private static List<String> getImgs(String urlStr) {
		List<String> list = new ArrayList<String>();
		URL url = null;
		URLConnection conn = null;
		String nextLine = null;
		StringTokenizer tokenizer = null;
		try {
			// 获得网页资源
			url = new URL(urlStr);
			// 获得资源连接
			conn = url.openConnection();
			conn.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			// 开始读取网页信息解析出网页中的超链接
			boolean flag = false;
			while ((nextLine = reader.readLine()) != null) {
				tokenizer = new StringTokenizer(nextLine);
				while (tokenizer.hasMoreTokens()) {
					String urlToken = tokenizer.nextToken();
					for (int i = 0, size = IMG_REGEXS.length; i < size; i++) {
						if (isMatcher(IMG_REGEXS[i], urlToken)) {
							flag = true;
							break;
						}
					}

					if (flag) {
						flag = false;
						String imgUrlStr = getWebPageImgs(urlToken);
						if (imgUrlStr != null) {
							list.add(imgUrlStr);
						}
					}
				}
			}

		} catch (Exception e) {
			System.out.println("==[ hava error ]==" + e);
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 判断字符串中是否含有超链接 刘柱栋 2013-3-4
	 * 
	 * @param urlToken
	 * @return
	 */
	public static boolean isUrl(String urlToken) {
		if (urlToken.indexOf("http") != -1) {
			return true;
		}
		return false;
	}

	/**
	 * 将字符串中超链接提取出来 刘柱栋 2013-3-4
	 * 
	 * @param urlToken
	 * @return
	 */
	public static String getHttp(String urlToken) {
		int start = urlToken.indexOf("http");
		int end = urlToken.length();
		String tempStr = urlToken.substring(start, end);
		end = tempStr.indexOf("\"");
		if (end == -1) {
			end = tempStr.length();
		}
		return tempStr.substring(0, end);
	}

	/**
	 * 将字符串的帖子标题提取出来 刘柱栋 2013-3-4
	 * 
	 * @param urlToken
	 * @return
	 */
	public static String getWebPageTopic(String urlToken) {
		int start = urlToken.indexOf("title=");
		int end = urlToken.indexOf("\">");
		String tempStr = urlToken.substring(start + 7, end);
		return tempStr;
	}

	public static String getWebPageImgs(String urlToken) {
		int start = urlToken.indexOf("\"");
		int end = urlToken.length() - 1;
		String tempStr = urlToken.substring(start + 1, end);
		if (urlToken.indexOf(".jpg") == -1) {
			tempStr += ".jpg";
		}
		return tempStr;
	}

	/**
	 * 使用正则表达式匹配指定的字符串.<br >
	 * 刘柱栋 2013-3-4
	 * 
	 * @param regEX
	 *            正则表达式
	 * @param str
	 *            要匹配的字符串
	 * @return
	 */
	private static boolean isMatcher(String regEX, String str) {
		Pattern p = Pattern.compile(regEX, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		return m.find();
	}

	public static void printList(List<String> list) {
		if (list != null) {
			StringBuilder strb = new StringBuilder(200);
			strb.append("============================================================== \r\n");
			for (String str : list) {
				saveImg(str);
				strb.append(str + "\r\n");
				count++;
				System.out.print("=");
			}
			System.out.println();
			strb.append("==============================================================");
			FileUtli.outPutStringToFile(strb.toString());
		}
	}

	public static void saveImg(String urlStr) {
		try {
			String imgSaveurl = "F://test5" + File.separator + getImgName(urlStr);
			File file = new File(imgSaveurl);
			if (file.exists()) {
				return;
			}
			urlStr = urlStr.trim();
			URL url = new URL(urlStr);
			BufferedImage image = ImageIO.read(url);
			ImageIO.write(image, "JPG", file);
		} catch (Exception e) {
			e.printStackTrace();
			FileUtli.outPutStringToFile(e.toString());
		}
	}

	private static String getImgName(String url) {
//		String[] strs = url.split("/");
//		int size = strs.length;
//		return dateStr + strs[size - 1];
		return dateStr + count + ".jpg";
	}
	
	/**
	 * 刘柱栋 2013-3-4
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String path = "F://test5" + File.separator + "logger.txt";
		FileUtli.setFilePath(path);
		start();
		System.out.println("总数：" + count);
		//saveImg("http://imgsrc.baidu.com/forum/pic/item/252be450352ac65cd9c03859faf2b21192138a7b.jpg");
	}
	
}
